package myPetStore.web.servlet;

import myPetStore.domain.Account;
import myPetStore.domain.Product;
import myPetStore.domain.Log;
import myPetStore.persistence.LogDao;
import myPetStore.persistence.impl.LogDaoImpl;
import myPetStore.service.AccountService;
import myPetStore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;
import java.util.Date;

public class RegisterServlet extends HttpServlet {

    //简单判断用户输入信息的合法性
    Account account=new Account();
    private String repeatePassword;
    private String userCode;            //用户输入的验证码
    private String verifiactionCode;    //生成的验证码
    private String msg;
    private AccountService accountService;
    private static final String  REGISTER_FORM="/WEB-INF/jsp/account/register.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        accountService=new AccountService();
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        //初始化account
        account.setUsername(req.getParameter("username"));
        account.setPassword(req.getParameter("password"));
        account.setFirstName(req.getParameter("firstname"));
        account.setLastName(req.getParameter("lastname"));
        account.setEmail(req.getParameter("email"));
        account.setCountry(req.getParameter("country"));
        account.setState(req.getParameter("state"));
        account.setCity(req.getParameter("city"));
        account.setAddress1(req.getParameter("address1"));
        account.setAddress2(req.getParameter("address2"));
        account.setZip(req.getParameter("zip"));
        account.setPhone(req.getParameter("phone"));
        account.setFavouriteCategoryId(req.getParameter("favcategory"));
        userCode=req.getParameter("token");             //用户输入的验证码




        HttpSession session= req.getSession();
        verifiactionCode=(String) session.getAttribute("verification"); //系统生成的验证码
        this.repeatePassword=req.getParameter("repeatPassword");
        String state="";
       if(req.getParameter("registerOnly")!=null&&!req.getParameter("registerOnly").equals("")){
           state=req.getParameter("registerOnly");
       }else{
           state=req.getParameter("registerAndLogin");
       }




        if(!isValide()){
            req.setAttribute("registerMsg",this.msg);
            out.print(this.msg);
        }else{
            //TODO 判断这个账号是否存在,有bug

//            accountService.addAccount(account);

            if(accountService.usernameIsExist(account.getUsername())){
                //有重复的账号名
                out.println("The username exists");
            }else {

                if(state.equals("registerOnly")){
                    out.println("address:signonForm");
//                resp.sendRedirect("signonForm");
                }else{
//                HttpSession session= req.getSession();
                    session.setAttribute("loginAccount",account);
                    if(account.isListOption()){
                        CatalogService catalogService=new CatalogService();
                        List<Product> productList=catalogService.getProductListByCategory(account.getFavouriteCategoryId());
                        session.setAttribute("myList",productList);
                    }
                    //提交地址
                    out.print("address:mainForm");
                    Log log = new Log();
                    log.setLogTime(new Date());
                    log.setUserName(account.getUsername());
                    log.setTitle("注册信息");
                    log.setContent("用户" + account.getUsername() + "注册了账号");
                    LogDao logDao = new LogDaoImpl();
                    logDao.InsertLog(log);
                }
            }

        }
    }



    private boolean isValide(){
        if(account.getUsername()==null||account.getUsername().equals("")){
            this.msg="The username cannot be empty";
            return false;
        }
        if(account.getPassword()==null||account.getPassword().equals("")){
            this.msg="The password cannot be empty";
            return false;
        }
        if(this.repeatePassword==null||this.repeatePassword.equals("")){
            this.msg="The repeat password cannot be empty";
            return false;
        }
        if(!Objects.equals(account.getPassword(), this.repeatePassword)){
            this.msg="The password is not the same between the two entries";
            return false;
        }
        if(account.getFirstName()==null||account.getFirstName().equals("")){
            this.msg="The firstname cannot be empty";
            return false;
        }
        if(account.getLastName()==null||account.getLastName().equals("")){
            this.msg="The lastname cannot be empty";
            return false;
        }
        if(account.getEmail()==null||account.getEmail().equals("")){
            this.msg="The email cannot be empty";
            return false;
        }
        //检查邮箱合法性：

        if(!account.getEmail().contains("@")){
            this.msg="Please enter a reasonable email";
            return false;
        }
        if(userCode==null||userCode.equals("")){
            this.msg="The code cannot be empty";
        }
        //检查验证码
        if(account.getCountry()==null||account.getCountry().equals("")){
            this.msg="The Country cannot be empty";
            return false;
        }
        if(account.getState()==null||account.getState().equals("")){
            this.msg="The State cannot be empty";
            return false;
        }
        if(account.getCity()==null||account.getCity().equals("")){
            this.msg="The City cannot be empty";
            return false;
        }
        if(account.getAddress1()==null||account.getAddress1().equals("")){
            this.msg="The Address1 cannot be empty";
            return false;
        }
        if(account.getAddress2()==null||account.getAddress2().equals("")){
            this.msg="The Address2 cannot be empty";
            return false;
        }
        if(account.getZip()==null||account.getZip().equals("")){
            this.msg="The zip code cannot be empty";
            return false;
        }
        if(account.getPhone()==null||account.getPhone().equals("")){
            this.msg="The phone cannot be empty";
            return false;
        }
        if(account.getFavouriteCategoryId()==null||account.getFavouriteCategoryId().equals("not")){
            this.msg="The FavouriteCategoryId cannot be empty";
            return false;
        }
        return true;
    }
}
